package com.example.agrigreennewssys.controller;


import com.example.agrigreennewssys.dto.ArticleDetailDTO;
import com.example.agrigreennewssys.entity.Article;
import com.example.agrigreennewssys.entity.ArticleLike;
import com.example.agrigreennewssys.service.ArticleService;
import com.example.agrigreennewssys.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Tag(name = "文章接口")
@RestController
@RequestMapping("/api/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    // 创建文章
    @PostMapping("/add")
    public Result createArticle(@RequestBody Article article) {
         articleService.createArticle(article);
        return Result.success("文章创建成功！");
    }
    // 编辑文章
    @PutMapping("/edit")
    public Result updateArticle(@RequestBody Article article) {
        System.out.println("ll:"+article);
        Article updatedArticle = articleService.updateArticle(article);
        return updatedArticle != null ? Result.success(updatedArticle) : Result.error(HttpStatus.NOT_FOUND.value(), "文章不存在");
    }
    // 更新文章状态
    @PutMapping("/status/{id}")
    public Result updateArticleStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean success = articleService.updateArticleStatus(id, status);
        return success ? Result.success() : Result.error(403,"更新文章状态失败");
    }
    // 删除文章
    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable Long id) {
        boolean deleted = articleService.deleteArticle(id);
        if (deleted) {
             return Result.success();
        }
         return Result.error(HttpStatus.NOT_FOUND.value(), "文章不存在");
    }

    // 获取文章详情
    @GetMapping("/{id}")
    public Result getArticleById(@PathVariable Long id) {
        Article article = articleService.getArticleById(id);
        if (article != null) {
             return Result .success(article);
        }
        return Result.error(HttpStatus.NOT_FOUND.value(), "文章不存在");
    }
    @GetMapping("/list")
    public List<ArticleDetailDTO> getArticleDetails(@RequestParam(required = false) String tagName, @RequestParam(required = false) Long authorId) {
        System.out.println("作者："+authorId);
        return articleService.getArticleDetails(tagName,authorId);
    }
    // 获取单个文章详情
    @GetMapping("/detail/{articleId}")
    public ArticleDetailDTO getArticleDetail(@PathVariable Long articleId) {
        return articleService.getArticleDetail(articleId);
    }

    // 获取所有文章
    @GetMapping
    public Result getAllArticles() {
        List<Article> articles = articleService.getAllArticles();
        return Result.success(articles);
    }

    // 根据标签获取文章
    @GetMapping("/by-tag/{tagId}")
    public Result getArticlesByTag(@PathVariable Long tagId) {
        List<Article> articles = articleService.getArticlesByTag(tagId);
        return Result.success(articles);
    }


    // 点赞文章
    @PostMapping("/like/{articleId}")
    public Result likeArticle(@PathVariable Long articleId, @RequestParam Long userId) {
        try {
            boolean success = articleService.likeArticle(articleId, userId);
            return success ? Result.success("点赞成功") : Result.error(400, "已点赞过");
        } catch (Exception e) {
            return Result.error(500, "点赞失败：" + e.getMessage());
        }
    }

    // 取消点赞文章
    @PostMapping("/unlike/{articleId}")
    public Result unlikeArticle(@PathVariable Long articleId, @RequestParam Long userId) {
        try {
            boolean success = articleService.unlikeArticle(articleId, userId);
            return success ? Result.success("取消点赞成功") : Result.error(400, "未点赞过");
        } catch (Exception e) {
            return Result.error(500, "取消点赞失败：" + e.getMessage());
        }
    }
    @GetMapping("/liked-status/{articleId}")
    public Result getArticleLikedStatus(@PathVariable Long articleId, @RequestParam Long userId) {
        boolean isLiked = articleService.isArticleLiked(articleId, userId);
        return Result.success(isLiked);
    }


    // 获取点赞列表（含关联信息）
    @PostMapping("/like/list")
    public Map<String,Object> getCommentLikeList(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit,@RequestBody ArticleLike articleLike) {
        PageHelper.startPage(page,limit);
        List<ArticleLike> articleLikeList=articleService.selectArticleLikeList(articleLike.getNickName(), articleLike.getArticleTitle());
        PageInfo<ArticleLike> pageInfo=new PageInfo<>(articleLikeList);
        Map<String,Object> result=new HashMap<>();
        result.put("code", 200);
        result.put("data", pageInfo.getList());
        result.put("msg", "查询成功");
        result.put("total", pageInfo.getTotal());
        return result;
    }

}
